package com.mathassistant.server;

import java.util.ArrayList;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mathassistant.client.service.ExerciseService;
import com.mathassistant.server.domain.Exercise;
import com.mathassistant.server.domain.Topic;
import com.mathassistant.shared.ExerciseDTO;
import com.mathassistant.shared.TopicDTO;

@SuppressWarnings("serial")
public class ExerciseServiceImpl  extends RemoteServiceServlet implements ExerciseService{

	@Override
	public void createExercise(ExerciseDTO exercise, TopicDTO topic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic tp = pm.getObjectById(Topic.class, topic.getId());
			Exercise ex = new Exercise(exercise);
			pm.makePersistent(ex);
			tp.getExercises().add(ex);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			pm.close();
		}
	}

	@Override
	public void updateExercise(ExerciseDTO exercise) {
		// TODO Auto-generated method stub
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Exercise ex = pm.getObjectById(Exercise.class, exercise.getId());
			ex.updateformDTO(exercise);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void deleteExercise(ExerciseDTO exercise) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Exercise ex = pm.getObjectById(Exercise.class, exercise.getId());
			if(ex != null){
				pm.deletePersistent(pm.getObjectById(Exercise.class, exercise.getId()));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
	
	
	@Override
	public int getCount(TopicDTO tp) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		int result = -1;
		try {
			Topic itp = pm.getObjectById(Topic.class, tp.getId());
			if(itp != null){
				ArrayList<Exercise> list = itp.getExercises();
				if(list != null){
					result = list.size();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			pm.close();
		}
		return result;
	}

	@Override
	public ExerciseDTO getNextExercise(int iCurrEx, TopicDTO tp) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ExerciseDTO result = null;
		try {
			Topic itp = pm.getObjectById(Topic.class, tp.getId());
			
			if(itp != null){
				result = new ExerciseDTO();
				ArrayList<Exercise> list = itp.getExercises();
				if(list != null){
					result = list.get(iCurrEx).toDTO();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			pm.close();
		}
		return result;
	}

	@Override
	public ArrayList<ExerciseDTO> getExercise(int iCurrEx, TopicDTO tp) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ArrayList<ExerciseDTO> result = new ArrayList<ExerciseDTO>();
		try {
			Topic itp = pm.getObjectById(Topic.class, tp.getId());
			if(itp != null){
				ArrayList<Exercise> list = itp.getExercises();
				
				if(list != null){
					for(int i = iCurrEx; i < iCurrEx+10; i++)
					{
						if(i>=list.size())
							break;
						result.add(list.get(i).toDTO());
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			pm.close();
		}
		return result;
	}
}
